package edu.neelsys.repository.college;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.mysema.query.BooleanBuilder;
import com.mysema.query.hql.HQLQuery;
import com.mysema.query.hql.jpa.JPAQuery;

import edu.neelsys.objectmodel.dbentites.CollegeSearchView;
import edu.neelsys.objectmodel.dbentites.QCollegeSearchView;

/**
 * Implementation of custom entity query methods. The example here uses QueryDsl
 * way so that queries are fully java complaint. However, there is a possiblity
 * to use named queries or simple string quereis as well here.
 * 
 * To Understand how to implement quereis do a search on QueryDsl examples in
 * google.
 * 
 * @author madhu
 * 
 */
public class CollegeSearchViewRepositoryImpl implements
		CollegeSearchViewRepoCustom {

	@PersistenceContext
	private EntityManager entityManager;

	public List<CollegeSearchView> findCollegeSearchView(String search)
			 {

		QCollegeSearchView collegeSearch = QCollegeSearchView.collegeSearchView;

		HQLQuery query = new JPAQuery(entityManager);

		query.from(collegeSearch);

		BooleanBuilder booleanBuilder = new BooleanBuilder();

		booleanBuilder.or(collegeSearch.name.like(search));
		booleanBuilder.or(collegeSearch.address.like(search));
		booleanBuilder.or(collegeSearch.city.like(search));
		booleanBuilder.or(collegeSearch.country.like(search));

		query.where(booleanBuilder);

		return query.list(collegeSearch);
	}

}
